<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function add_draw($title, $description, $user_id, $shared,
                        $width, $height, $orientation,
                        $device_model, $device_uid,
                        $md5, $url, $thumbnail_url,
                        $lat, $lng, $location_description) {
    
    $title                  = mysql_real_escape_string($title);
    $description            = mysql_real_escape_string($description);
    $user_id                = mysql_real_escape_string($user_id);
    $shared                 = mysql_real_escape_string($shared);
    $width                  = mysql_real_escape_string($width);
    $height                 = mysql_real_escape_string($height);
    $orientation            = mysql_real_escape_string($orientation);
    $device_model           = mysql_real_escape_string($device_model);
    $device_uid             = mysql_real_escape_string($device_uid);
    $md5                    = mysql_real_escape_string($md5);
    $lat                    = mysql_real_escape_string($lat);
    $lng                    = mysql_real_escape_string($lng);
    $location_description   = mysql_real_escape_string($location_description);
    $url                    = mysql_real_escape_string($url);
    $thumbnail_url          = mysql_real_escape_string($thumbnail_url);
    
    if(strlen($md5) <= 0 || strlen($url) <= 0 || strlen($title) <= 0) {
        return FALSE;
    }
    $timestamp = time();
    $query = "INSERT INTO draws(title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, timestamp)
                VALUES('$title', '$description', '$user_id', '$shared',
                        '$width', '$height', '$orientation',
                        '$device_model', '$device_uid',
                        '$md5', '$url', '$thumbnail_url',
                        '$lat', '$lng', '$location_description', '$timestamp')";
    //echo $query;
    return safe_insert_query($query);
}

function claim_draw($title, $description, $user_id, $shared,
                        $width, $height, $orientation,
                        $device_model, $device_uid,
                        $md5, $url, $thumbnail_url,
                        $lat, $lng, $location_description,
                        $timestamp, $likes) {
    
    $title                  = mysql_real_escape_string($title);
    $description            = mysql_real_escape_string($description);
    $user_id                = mysql_real_escape_string($user_id);
    $shared                 = mysql_real_escape_string($shared);
    $width                  = mysql_real_escape_string($width);
    $height                 = mysql_real_escape_string($height);
    $orientation            = mysql_real_escape_string($orientation);
    $device_model           = mysql_real_escape_string($device_model);
    $device_uid             = mysql_real_escape_string($device_uid);
    $md5                    = mysql_real_escape_string($md5);
    $lat                    = mysql_real_escape_string($lat);
    $lng                    = mysql_real_escape_string($lng);
    $location_description   = mysql_real_escape_string($location_description);
    $url                    = mysql_real_escape_string($url);
    $thumbnail_url          = mysql_real_escape_string($thumbnail_url);
    
    $timestamp              = mysql_real_escape_string($timestamp);
    $likes                  = mysql_real_escape_string($likes);
    
    if(!$timestamp || strlen($md5) <= 0 || strlen($url) <= 0 || strlen($title) <= 0) {
        return FALSE;
    }
    
    $query = "INSERT INTO draws(title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, timestamp, likes)
                VALUES('$title', '$description', '$user_id', '$shared',
                        '$width', '$height', '$orientation',
                        '$device_model', '$device_uid',
                        '$md5', '$url', '$thumbnail_url',
                        '$lat', '$lng', '$location_description', '$timestamp', '$likes')";
    //echo $query;
    return safe_insert_query($query);
}

function port_draw($id, $title, $description, $user_id, $shared,
                        $width, $height, $orientation,
                        $device_model, $device_uid,
                        $md5, $url, $thumbnail_url,
                        $lat, $lng, $location_description, $timestamp, $likes) {
    
    $title                  = mysql_real_escape_string($title);
    $description            = mysql_real_escape_string($description);
    $user_id                = mysql_real_escape_string($user_id);
    $shared                 = mysql_real_escape_string($shared);
    $width                  = mysql_real_escape_string($width);
    $height                 = mysql_real_escape_string($height);
    $orientation            = mysql_real_escape_string($orientation);
    $device_model           = mysql_real_escape_string($device_model);
    $device_uid             = mysql_real_escape_string($device_uid);
    $md5                    = mysql_real_escape_string($md5);
    $lat                    = mysql_real_escape_string($lat);
    $lng                    = mysql_real_escape_string($lng);
    $location_description   = mysql_real_escape_string($location_description);
    $url                    = mysql_real_escape_string($url);
    $thumbnail_url          = mysql_real_escape_string($thumbnail_url);
    
    $timestamp              = mysql_real_escape_string($timestamp);
    $likes                  = mysql_real_escape_string($likes);
    
    if(!$id || strlen($md5) <= 0 || strlen($url) <= 0) {
        return FALSE;
    }
    $query = "INSERT INTO draws(id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, timestamp, likes)
                VALUES('$id', '$title', '$description', '$user_id', '$shared',
                        '$width', '$height', '$orientation',
                        '$device_model', '$device_uid',
                        '$md5', '$url', '$thumbnail_url',
                        '$lat', '$lng', '$location_description', '$timestamp', '$likes')";
    //echo $query;
    return safe_insert_query($query);
}

function get_draws_by_owner_id($user_id, $owner_id, $limit, $offset) {
    $user_id = mysql_real_escape_string($user_id);
    $owner_id = mysql_real_escape_string($owner_id);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws 
                WHERE user_id = '$owner_id'
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)    
                ORDER BY id DESC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_draws_by_owner_ids($user_id, $owner_ids, $limit, $offset) {
    $user_id = mysql_real_escape_string($user_id);
    
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    if(!is_array($owner_ids) || count($owner_ids) <= 0) {
        return array();
    }
    $owner_id_list = "(0";
    foreach($owner_ids as $owner_id) {
        $owner_id = mysql_real_escape_string($owner_id);
        $owner_id_list .= " ,'$owner_id'";
    }
    $owner_id_list .= ")";
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws 
                WHERE user_id IN $owner_id_list
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)    
                ORDER BY id DESC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_draws_by_owner_id_reverse($user_id, $owner_id, $limit, $offset) {
    $user_id = mysql_real_escape_string($user_id);
    $owner_id = mysql_real_escape_string($owner_id);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws 
                WHERE user_id = '$owner_id'
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)    
                ORDER BY id ASC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_draws_by_owner_ids_reverse($user_id, $owner_ids, $limit, $offset) {
    $user_id = mysql_real_escape_string($user_id);
    
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    if(!is_array($owner_ids) || count($owner_ids) <= 0) {
        return array();
    }
    $owner_id_list = "(0";
    foreach($owner_ids as $owner_id) {
        $owner_id = mysql_real_escape_string($owner_id);
        $owner_id_list .= " ,'$owner_id'";
    }
    $owner_id_list .= ")";
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws 
                WHERE user_id IN $owner_id_list
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)    
                ORDER BY id ASC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_draw_by_id($id) {
    $id = mysql_real_escape_string($id);
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws WHERE id = '$id' LIMIT 0,1";
    //echo $query;
    
    $result = safe_select_query($query);
    return $result[0];
}

function get_draws_by_ids($ids) {
    if(!is_array($ids) || count($ids) <= 0) {
        return array();
    }
    $id_list = "(0";
    foreach($ids as $id) {
        $id = mysql_real_escape_string($id);
        $id_list .= " ,'$id'";
    }
    $id_list .= ")";
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws 
                WHERE id IN '$id_list'
                ORDER BY id DESC";
    //echo $query;
    return safe_select_query($query);
}

function get_popular_draws($user_id, $limit, $offset) {
    $user_id = mysql_real_escape_string($user_id);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                ORDER BY likes DESC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_recent_draws($user_id, $limit, $offset) {
    $user_id = mysql_real_escape_string($user_id);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                ORDER BY timestamp DESC, likes DESC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_least_recent_draws($user_id, $limit, $offset) {
    $user_id = mysql_real_escape_string($user_id);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                ORDER BY timestamp ASC, likes DESC 
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_nearby_draws($user_id, $lat, $lng, $distance, $limit, $offset) {
    $user_id = mysql_real_escape_string($user_id);
    $lat = mysql_real_escape_string($lat);
    $lng = mysql_real_escape_string($lng);
    $distance = mysql_real_escape_string($distance);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $epsilon = sqrt($distance * $distance / 7570);
    $min_lat = $lat - $epsilon;
    $min_lng = $lng - $epsilon;
    $max_lat = $lat + $epsilon;
    $max_lng = $lng + $epsilon;
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp,
                        ( 6371 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) as distance
                FROM draws
                WHERE lat >= '$min_lat' AND lat <= '$max_lat' AND lng >= '$min_lng' AND lng <= '$max_lng'
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                HAVING distance <= $distance
                ORDER BY distance, timestamp DESC, likes DESC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_popular_draws_with_keywords($user_id, $limit, $offset, $keywords) {
    $user_id = mysql_real_escape_string($user_id);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $names = $keywords;
    $likes_query = "1";
    $likes_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) <= 3 && strlen($name) > 0) {
            $likes_count++;
            $likes_query .= " AND (title LIKE '%$name%' OR description LIKE '%$name%')";
        }
    }
    if(!$likes_count) {
        $likes_query = 0;
    }
    $names_query = "MATCH(title, description) AGAINST ('1";
    $names_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) > 3) {
            $names_query .= " $name";
            $names_count++;
        }
    }
    $names_query .= "')";
    if(!$names_count) {
        $names_query = 0;
    }
    if(!$names_count && !$likes_count) {
        $names_query = 1;
    }
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                AND (($names_query) OR ($likes_query))
                ORDER BY likes DESC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_recent_draws_with_keywords($user_id, $limit, $offset, $keywords) {
    $user_id = mysql_real_escape_string($user_id);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $names = $keywords;
    $likes_query = "1";
    $likes_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) <= 3 && strlen($name) > 0) {
            $likes_count++;
            $likes_query .= " AND (title LIKE '%$name%' OR description LIKE '%$name%')";
        }
    }
    if(!$likes_count) {
        $likes_query = 0;
    }
    $names_query = "MATCH(title, description) AGAINST ('1";
    $names_count = 0;
    foreach($names as $name) {
        if(strlen($name) > 3) {
            $names_query .= " $name";
            $names_count++;
        }
    }
    $names_query .= "')";
    if(!$names_count) {
        $names_query = 0;
    }
    if(!$names_count && !$likes_count) {
        $names_query = 1;
    }
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                AND (($names_query) OR ($likes_query))
                ORDER BY timestamp DESC, likes DESC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_least_recent_draws_with_keywords($user_id, $limit, $offset, $keywords) {
    $user_id = mysql_real_escape_string($user_id);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $names = $keywords;
    $likes_query = "1";
    $likes_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) <= 3 && strlen($name) > 0) {
            $likes_count++;
            $likes_query .= " AND (title LIKE '%$name%' OR description LIKE '%$name%')";
        }
    }
    if(!$likes_count) {
        $likes_query = 0;
    }
    $names_query = "MATCH(title, description) AGAINST ('1";
    $names_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) > 3) {
            $names_query .= " $name";
            $names_count++;
        }
    }
    $names_query .= "')";
    if(!$names_count) {
        $names_query = 0;
    }
    if(!$names_count && !$likes_count) {
        $names_query = 1;
    }
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                AND (($names_query) OR ($likes_query))
                ORDER BY timestamp ASC, likes DESC 
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

function get_nearby_draws_with_keywords($user_id, $lat, $lng, $distance, $limit, $offset, $keywords) {
    $user_id = mysql_real_escape_string($user_id);
    $lat = mysql_real_escape_string($lat);
    $lng = mysql_real_escape_string($lng);
    $distance = mysql_real_escape_string($distance);
    $limit = mysql_real_escape_string($limit);
    $offset = mysql_real_escape_string($offset);
    $names = $keywords;
    $likes_query = "1";
    $likes_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) <= 3 && strlen($name) > 0) {
            $likes_count++;
            $likes_query .= " AND (title LIKE '%$name%' OR description LIKE '%$name%')";
        }
    }
    if(!$likes_count) {
        $likes_query = 0;
    }
    $names_query = "MATCH(title, description) AGAINST ('1";
    $names_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) > 3) {
            $names_query .= " $name";
            $names_count++;
        }
    }
    $names_query .= "')";
    if(!$names_count) {
        $names_query = 0;
    }
    if(!$names_count && !$likes_count) {
        $names_query = 1;
    }
    $epsilon = sqrt($distance * $distance / 7570);
    $min_lat = $lat - $epsilon;
    $min_lng = $lng - $epsilon;
    $max_lat = $lat + $epsilon;
    $max_lng = $lng + $epsilon;
    $query = "SELECT id, title, description, user_id, shared,
                        width, height, orientation,
                        device_model, device_uid,
                        md5, url, thumbnail_url,
                        lat, lng, location_description, likes, timestamp,
                        ( 6371 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) as distance
                FROM draws
                WHERE lat >= '$min_lat' AND lat <= '$max_lat' AND lng >= '$min_lng' AND lng <= '$max_lng'
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                AND (($names_query) OR ($likes_query))
                HAVING distance <= $distance
                ORDER BY distance, timestamp DESC, likes DESC
                LIMIT $offset, $limit";
    //echo $query;
    return safe_select_query($query);
}

// ----- COUNT

function count_draws_by_owner_id($user_id, $owner_id) {
    $user_id = mysql_real_escape_string($user_id);
    $owner_id = mysql_real_escape_string($owner_id);
    $query = "SELECT count(id) as total
                FROM draws 
                WHERE user_id = '$owner_id'
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_draws_by_owner_ids($user_id, $owner_ids) {
    $user_id = mysql_real_escape_string($user_id);
    if(!is_array($owner_ids) || count($owner_ids) <= 0) {
        return array();
    }
    $owner_id_list = "(0";
    foreach($owner_ids as $owner_id) {
        $owner_id = mysql_real_escape_string($owner_id);
        $owner_id_list .= " ,'$owner_id'";
    }
    $owner_id_list .= ")";
    $query = "SELECT count(id) as total
                FROM draws 
                WHERE user_id IN $owner_id_list
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_popular_draws($user_id) {
    $user_id = mysql_real_escape_string($user_id);
    $query = "SELECT count(id) as total
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_recent_draws($user_id) {
    $user_id = mysql_real_escape_string($user_id);
    $query = "SELECT count(id) as total
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_least_recent_draws($user_id) {
    $user_id = mysql_real_escape_string($user_id);
    $query = "SELECT count(id) as total
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_nearby_draws($user_id, $lat, $lng, $distance) {
    $user_id = mysql_real_escape_string($user_id);
    $lat = mysql_real_escape_string($lat);
    $lng = mysql_real_escape_string($lng);
    $distance = mysql_real_escape_string($distance);
    $epsilon = sqrt($distance * $distance / 7570);
    $min_lat = $lat - $epsilon;
    $min_lng = $lng - $epsilon;
    $max_lat = $lat + $epsilon;
    $max_lng = $lng + $epsilon;
    $query = "SELECT count(id) as total
                FROM draws
                WHERE lat >= '$min_lat' AND lat <= '$max_lat' AND lng >= '$min_lng' AND lng <= '$max_lng'
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                AND ( 6371 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) <= $distance";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_whiteboard_user_likes($user_id) {
    $user_id = mysql_real_escape_string($user_id);
    $query = "SELECT IFNULL(sum(likes), 0) as total
                FROM draws
                WHERE user_id = '$user_id'";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_popular_draws_with_keywords($user_id, $keywords) {
    $user_id = mysql_real_escape_string($user_id);
    $names = $keywords;
    $likes_query = "1";
    $likes_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) <= 3 && strlen($name) > 0) {
            $likes_count++;
            $likes_query .= " AND (title LIKE '%$name%' OR description LIKE '%$name%')";
        }
    }
    if(!$likes_count) {
        $likes_query = 0;
    }
    $names_query = "MATCH(title, description) AGAINST ('1";
    $names_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) > 3) {
            $names_query .= " $name";
            $names_count++;
        }
    }
    $names_query .= "')";
    if(!$names_count) {
        $names_query = 0;
    }
    if(!$names_count && !$likes_count) {
        $names_query = 1;
    }
    $query = "SELECT count(id) as total
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                AND (($names_query) OR ($likes_query))";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_recent_draws_with_keywords($user_id, $keywords) {
    $user_id = mysql_real_escape_string($user_id);
    $names = $keywords;
    $likes_query = "1";
    $likes_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) <= 3 && strlen($name) > 0) {
            $likes_count++;
            $likes_query .= " AND (title LIKE '%$name%' OR description LIKE '%$name%')";
        }
    }
    if(!$likes_count) {
        $likes_query = 0;
    }
    $names_query = "MATCH(title, description) AGAINST ('1";
    $names_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) > 3) {
            $names_query .= " $name";
            $names_count++;
        }
    }
    $names_query .= "')";
    if(!$names_count) {
        $names_query = 0;
    }
    if(!$names_count && !$likes_count) {
        $names_query = 1;
    }
    $query = "SELECT count(id) as total
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                AND (($names_query) OR ($likes_query))";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_least_recent_draws_with_keywords($user_id, $keywords) {
    $names = $keywords;
    $likes_query = "1";
    $likes_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) <= 3 && strlen($name) > 0) {
            $likes_count++;
            $likes_query .= " AND (title LIKE '%$name%' OR description LIKE '%$name%')";
        }
    }
    if(!$likes_count) {
        $likes_query = 0;
    }
    $names_query = "MATCH(title, description) AGAINST ('1";
    $names_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) > 3) {
            $names_query .= " $name";
            $names_count++;
        }
    }
    $names_query .= "')";
    if(!$names_count) {
        $names_query = 0;
    }
    if(!$names_count && !$likes_count) {
        $names_query = 1;
    }
    $query = "SELECT count(id) as total
                FROM draws
                WHERE id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                AND (($names_query) OR ($likes_query))";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_nearby_draws_with_keywords($user_id, $lat, $lng, $distance, $keywords) {
    $user_id = mysql_real_escape_string($user_id);
    $lat = mysql_real_escape_string($lat);
    $lng = mysql_real_escape_string($lng);
    $distance = mysql_real_escape_string($distance);
    $names = $keywords;
    $likes_query = "1";
    $likes_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) <= 3 && strlen($name) > 0) {
            $likes_count++;
            $likes_query .= " AND (title LIKE '%$name%' OR description LIKE '%$name%')";
        }
    }
    if(!$likes_count) {
        $likes_query = 0;
    }
    $names_query = "MATCH(title, description) AGAINST ('1";
    $names_count = 0;
    foreach($names as $name) {
        $name = mysql_real_escape_string($name);
        if(strlen($name) > 3) {
            $names_query .= " $name";
            $names_count++;
        }
    }
    $names_query .= "')";
    if(!$names_count) {
        $names_query = 0;
    }
    if(!$names_count && !$likes_count) {
        $names_query = 1;
    }
    $epsilon = sqrt($distance * $distance / 7570);
    $min_lat = $lat - $epsilon;
    $min_lng = $lng - $epsilon;
    $max_lat = $lat + $epsilon;
    $max_lng = $lng + $epsilon;
    $query = "SELECT count(id) as total
                FROM draws
                WHERE lat >= '$min_lat' AND lat <= '$max_lat' AND lng >= '$min_lng' AND lng <= '$max_lng'
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_reports WHERE user_id = '$user_id')
                AND id NOT IN (SELECT DISTINCT object_id FROM draw_abuses)
                AND ( 6371 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) <= $distance
                AND (($names_query) OR ($likes_query))";
    //echo $query;
    $result = safe_select_query($query);
    return $result[0]->total;
}

?>
